<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$domain = 'xiu.jinyunweb.com';
//获取独立域名
$jy_xiu=core_setting('jy_xiu');
$cloud=core_setting('cloud');
if($cloud['group'] && $jy_xiu['domain']){
	$domain = $jy_xiu['domain'];
}
$return['indexurl']=HTTP.$domain.'/index.php';
$return['post']=array();
if($_RQ['op']=='list'){
	$linkurl= HTTP.$domain.'/index.php';
	$url  = '?s=home/index/getscenelist/&';
	$res=$this->model('model')->xiu_request($url);
	$list=$res['list']['list'];
	$query=$res['query'];
	foreach($list as &$item){
		$item['cover']=HTTP.$domain.'/Uploads/'.$item['cover'];
		$item['url']=HTTP.$domain.'/v/'.$item['code'];
		$item['statustitle']=$item['status']==0?'未发布':'';
	}
	$return['list']=$list;
	$return['editurl']=HTTP.$domain.'/index.php?'.$query.'/#/scene/create/';
	$return['query']=$query;
	$return['isfounder']=$_SESSION['isfounder'];
	$this->session('query',$query);
	exi($return);
}
if($_RQ['op']=='tpllist'){
	$url  = '?s=home/index/getsyslist/&';
	$params=array();
	if($_RQ['page']){
		$params['page']=intval($_RQ['page']);
		$return['params']['page'] =intval($_RQ['page']);
	}
	if($_RQ['type']){
		$params['type']=intval($_RQ['type']);
		$return['params']['type'] =intval($_RQ['type']);
	}else{
		$return['params']['type']=0;
	}
	$res=$this->model('model')->xiu_request($url,$params);
	//print_r($res);exit;
	$list=$res['list'];
	$query=$res['query'];
	$total=$list['map']['count'];
	$psize=$list['map']['pageSize'];
	$list=$list['list'];
	$pindex = max(1, intval($_RQ['page']));
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	foreach($list as &$item){
		$item['cover']=HTTP.$domain.'/Uploads/'.$item['cover'];
		$item['url']=HTTP.$domain.'/v/'.$item['code'];
	}
	$return['list']=$list;
	$return['cates']=$res['list']['cates'];
	$this->session('cates',$return['cates']);
	$return['editurl']=HTTP.$domain.'/index.php?'.$query.'/#/scene/create/';
	$return['query']=$query;
	$return['istpl']=$istpl;
	exi($return);
}
if($_RQ['op']=='formdata'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要查看的场景！','error');
	}
	$url  = '?s=home/index/getformlist/&id='.$id.'&';
	$params=array('id'=>$id);
	if($_RQ['page']){
		$params['page']=intval($_RQ['page']);
		$return['params']['page'] =intval($_RQ['page']);
	}
	if($_RQ['type']){
		$params['type']=intval($_RQ['type']);
		$return['params']['type'] =intval($_RQ['type']);
	}else{
		$return['params']['type']=0;
	}
	$res=$this->model('model')->xiu_request($url,$params);
	$list=$res['list']['list'];
	if($list){
		$htmllist=array();
		foreach($list[0] as $n=>&$row){
			$htmllist[$n]['name']='name'.$n;
			$htmllist[$n]['title']=$row;
			$htmllist[$n]['type']='string';
		}
		unset($list[0]);
		$newlist=array();
		foreach($list as $k=>&$item){
			foreach($item as $kv=>&$v){
				$newlist[$k]['name'.$kv]=$v;
			}
		}
	}
	$return['list']=$newlist?$newlist:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','del_one'=>false,'delall'=>false,'edit'=>false,'list'=>$htmllist),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='msglist'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要查看的场景！','error');
	}
	$url  = '?s=home/index/getmsglist/&id='.$id.'&';
	$params=array('id'=>$id);
	if($_RQ['page']){
		$params['page']=intval($_RQ['page']);
		$return['params']['page'] =intval($_RQ['page']);
	}
	if($_RQ['type']){
		$params['type']=intval($_RQ['type']);
		$return['params']['type'] =intval($_RQ['type']);
	}else{
		$return['params']['type']=0;
	}
	$res=$this->model('model')->xiu_request($url,$params);
	$list=$res['list']['list'];
	$return['list']=$newlist?$newlist:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','del_one'=>false,'delall'=>false,'edit'=>false,'list'=>array(
				array('name'=>'id','title'=>'场景ID','type'=>'string'),
				array('name'=>'name','title'=>'用户名称','type'=>'string'),
				array('name'=>'url','title'=>'用户头像','type'=>'image'),
				array('name'=>'content','title'=>'留言内容','type'=>'string'),
				array('name'=>'createTime','title'=>'留言时间','type'=>'string'),
			)
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='transferscene'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('请先选择要转让的场景！','error');
		}
		if(!$post['userid_int']){
			exi('请先选择转赠账号ID！','error');
		}
		if($post['userid_int'] == $post['myid']){
			exi('不能转赠给自己！','error');
		}
		$url="?s=home/index/transfer/&userid_int=".$post['userid_int'].'&id='.$id.'&platform='.$post['platform'].'&';
		$res=$this->model('model')->xiu_request($url);
		//print_r($res);exit;
		if($res['list']['success']){
			exi('转赠成功！','','list');
		}else{
			exi($res['list']['msg'],'error');
		}
	}else{
		$id=intval($_RQ['id']);
		if(!$id){
			exi('请先选择要转赠的场景！','error');
		}
		$return['item']=array('id'=>$id,'myid'=>intval($_RQ['myid']));
		$return['htmls']=array(
			'type'=>'edit',
			'desc'=>'转赠场景',
			'edit'=>array(
				array('type'=>'string','name'=>'userid_int','title'=>'转赠账号ID','help'=>'转出后将不再拥有此场景'),
				array('type'=>'copy','name'=>'myid','title'=>'你的账号ID','help'=>'这里是你的ID，可以提供给转赠者'),
			),
		);
		exi($return);
	}
}
if($_RQ['op']=='copyscene'){
	$url='?s=home/index/createByCopy/&id='.intval($_RQ['id']).'&';
	$res=$this->model('model')->xiu_request($url,$params);
	if($res['list']['obj']){
		$return['id']=$res['list']['obj'];
		exi($return);
	}else{
		exi($res['list']['msg'],'error');
	}
}
if($_RQ['op']=='addscene'){
	$return['cates']=$this->session('cates');
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!$post['type']){
			exi('请先选择分类！','error');
		}
		$url='?s=home/index/addscene/&type='.$post['type'].'&';
		$res=$this->model('model')->xiu_request($url);
		if($res['list']['obj']){
			$return['id']=$res['list']['obj'];
			exi($return);
		}else{
			$res['list']['msg']=$res['list']['msg']?$res['list']['msg']:'创建失败';
			exi($res['list']['msg'],'error');
		}
	}else{
		$return['item']=array('type'=>$return['cates'][0]['value']);
		$return['htmls']=array(
			'type'=>'edit',
			'desc'=>'创建场景',
			'edit'=>array(
				array('type'=>'select','name'=>'type','title'=>'选择分类','options'=>'cates','option_value'=>'value'),
			),
		);
		exi($return);
	}
}
if($_RQ['op']=='createscene'){
	$return['cates']=$this->session('cates');
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('请先选择要创建的场景！','error');
		}
		if(!$post['type']){
			exi('请先选择分类！','error');
		}
		$url='?s=home/index/createBysys/&id='.$id.'&type='.$post['type'].'&';
		$res=$this->model('model')->xiu_request($url);
		if($res['list']['obj']){
			$return['id']=$res['list']['obj'];
			exi($return);
		}else{
			$res['list']['msg']=$res['list']['msg']?$res['list']['msg']:'创建失败';
			exi($res['list']['msg'],'error');
		}
	}else{
		$id=intval($_RQ['id']);
		if(!$id){
			exi('请先选择要创建的场景！'.$_RQ['type'],'error');
		}
		$return['item']=array('id'=>$id,'type'=>$_RQ['type']);
		$return['htmls']=array(
			'type'=>'edit',
			'desc'=>'创建场景',
			'edit'=>array(
				array('type'=>'select','name'=>'type','title'=>'选择分类','options'=>'cates','option_value'=>'value'),
			),
		);
		exi($return);
	}
}
if($_RQ['op']=='delscene'){
	$url='?s=home/index/delscene/&id='.intval($_RQ['id']).'&';
	//exi('~~~'.intval($_RQ['id']),'error');
	$res=$this->model('model')->xiu_request($url);
	//print_r($res);exit;
	exi('删除成功！');
}
if($_RQ['op']=='help'){
	$return['htmls']=array(
			'type'=>'look',
			'desc'=>'场景秀小技巧教程',
			'tabs'=>$tabs,
			'look'=>array(
				array('type'=>'tip','title'=>'【温馨提示】：活用场景秀，基本可以满足绝大部分场景需求！'),
				array('type'=>'help','title'=>'一、如何适屏幕大小：','help'=>'
					1、要背景自适应屏幕大小，不能简单的加一张图片把他拉大。要点击编辑器-上面菜单栏-背景-选择图片作为背景图，这样就会自适应屏幕大小了；</br>
				'),
				array('type'=>'help','title'=>'二、如何设置视频场景：','help'=>'
					1、点击编辑器-上面菜单栏-视频-复制视频通用代码；</br>
				'),
				array('type'=>'help','title'=>'三、如何设置表单场景：','help'=>'
					1、点击编辑器-上面菜单栏-表单-按要求填写表单的各项内容即可；</br>
				'),
			),
	);
	exi($return);
}
?>